package com.rich.login.service.impl;

import com.rich.common.constant.Broker;
import com.rich.common.entity.TbBroker;
import com.rich.common.entity.TbUser;
import com.rich.common.result.CommonResponse;
import com.rich.common.util.JwtUtil;
import com.rich.login.mapper.BrokerMapper;
import com.rich.login.service.BrokerService;
import com.rich.login.vo.BrokerPhoneLoginVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author:张龙飞
 * @Date:2021/8/14
 */
@Service
public class BrokerImpl implements BrokerService {
    @Autowired
    private BrokerMapper brokerMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public CommonResponse getCode(String phone) {
        String redisCode = (String) redisTemplate.opsForValue().get(phone + Broker.REDIS_YZM_MIN);
        if (StringUtils.isEmpty(redisCode)){
            Integer code = createCode();
            redisTemplate.opsForValue().set(phone+Broker.REDIS_YZM_MIN,code.toString(),5, TimeUnit.MINUTES);
            HashMap<String, String> map = new HashMap<>(64);
            map.put("phone",phone);
            map.put("code",code.toString());
            System.out.println(phone+"===="+code);
            rabbitTemplate.convertAndSend("coco",map);
            return new CommonResponse(Broker.REDIS_CODE_NOEXIT,Broker.REDIS_CODE_NOMESSAGE);
        }
        return new CommonResponse(Broker.REDIS_CODE_EXIT,Broker.REDIS_CODE_MESSAGE);
    }

    /**
     * 手机号验证码登录
     * @param userLoginVo
     * @return
     */
    @Override
    public CommonResponse dologin(BrokerPhoneLoginVo userLoginVo) {
        TbBroker broker=  brokerMapper.findByPhone(userLoginVo.getPhone());
        if (broker!=null){
            String redisCode = (String) redisTemplate.opsForValue().get(userLoginVo.getPhone() + Broker.REDIS_YZM_MIN);
            if (userLoginVo.getCode().equals(redisCode)){
                //登陆成功
                String uuid = UUID.randomUUID().toString();
                String loginToken = uuid.replace("-", "");
                String token= JwtUtil.createToken(loginToken,new Date());
                redisTemplate.opsForValue().set(loginToken,broker,30, TimeUnit.MINUTES);
                return new CommonResponse(Broker.BROKER_LOGIN_SUCCESS,Broker.BROKER_LOGIN_SUCCESS_MESSAGE,token);
            }
            return new CommonResponse(Broker.REDIS_CODE_NO,Broker.REDIS_CODE_NOMESSAGE_EOOR);
        }
        return new CommonResponse(Broker.USER_EXIST_CODE,Broker.USER_EXIST_MESSAGE);
    }
    private Integer createCode() {
        return (int) (Math.random()*(Broker.USER_YZM_MAX - Broker.USER_YZM_MIN) + Broker.USER_YZM_MIN);
    }
}
